package parabullgame;


import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.input.Mouse;
import java.awt.Color;

import static org.lwjgl.opengl.GL11.*;

public class Game {

    static GuiControlBase baseControl;

    public static void main(String[] args) throws LWJGLException {

        //Toolkit toolkit =  Toolkit.getDefaultToolkit ();
        //Dimension dim = toolkit.getScreenSize();

        int width = 640;
        int height = 580;

        //int width = dim.width;
        //int height = dim.height;

        DisplayMode d = Display.getAvailableDisplayModes()[85];
        setDisplayMode(width, height,false);
        Display.setFullscreen(true);
        
        baseControl = new GuiControlBase(0,0,width,height);
        GuiControlWindow test = new GuiControlWindow(10,10,200,200);
        test.addChild(new GuiControlButton(30,30,100,100));
        baseControl.addChild(test);

        Display.create();

        glEnable(GL_TEXTURE); //Need textures for gam
        glDisable(GL_DEPTH_TEST); //2D gam, no need

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, width, height, 0, -1, 1);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        //glViewport(0, 0, width, height);

        //int i = 1;
        Map myMap = new Map();
        myMap.addEntity(new GraphLine(200d,200d,100d,0d,Color.yellow));
        
        while (!Display.isCloseRequested()) {
            //Input
            while(Mouse.next()) {
                if (Mouse.getEventButton() != -1) {
                    baseControl.mouseClickInternal(Mouse.getEventButton(),Mouse.getEventButtonState(),Mouse.getEventX(),height-Mouse.getEventY());
                }
            }

            baseControl.thinkInternal(Mouse.getX(),height-Mouse.getY());
            
            //Drawing
            glClear(GL_COLOR_BUFFER_BIT);
            myMap.drawList();

            //DrawUtil.circle(width / 2, height / 2 - i, i, new Color(0, 0, 150));
            baseControl.drawInternal(0,0);
            Display.update();

            //i++;
        }
        Display.destroy();
    }

    //"Borrowed" this function from http://lwjgl.org/wiki/index.php?title=LWJGL_Basics_5_%28Fullscreen%29 .  Has come in handy :3.
    static void setDisplayMode(int width, int height, boolean fullscreen) {

        // return if requested DisplayMode is already set
        if ((Display.getDisplayMode().getWidth() == width)
                && (Display.getDisplayMode().getHeight() == height)
                && (Display.isFullscreen() == fullscreen)) {
            return;
        }

        try {
            DisplayMode targetDisplayMode = null;

            if (fullscreen) {
                DisplayMode[] modes = Display.getAvailableDisplayModes();
                int freq = 0;

                for (int i = 0; i < modes.length; i++) {
                    DisplayMode current = modes[i];

                    if ((current.getWidth() == width) && (current.getHeight() == height)) {
                        if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
                            if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) {
                                targetDisplayMode = current;
                                freq = targetDisplayMode.getFrequency();
                            }
                        }

                        // if we've found a match for bpp and frequence against the 
                        // original display mode then it's probably best to go for this one
                        // since it's most likely compatible with the monitor
                        if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel())
                                && (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) {
                            targetDisplayMode = current;
                            break;
                        }
                    }
                }
            } else {
                targetDisplayMode = new DisplayMode(width, height);
            }

            if (targetDisplayMode == null) {
                System.out.println("Failed to find value mode: " + width + "x" + height + " fs=" + fullscreen);
                return;
            }

            Display.setDisplayMode(targetDisplayMode);
            Display.setFullscreen(fullscreen);

        } catch (LWJGLException e) {
            System.out.println("Unable to setup mode " + width + "x" + height + " fullscreen=" + fullscreen + e);
        }
    }
}
